#include "cpu.h"
#include "typedef.h"
#include "switch_driver.h"



struct SWITCH_GPIO_S
{
	GPIO_TypeDef* GPIOx;
	u16 GPIO_Pin;
	GPIOMode_TypeDef mode;
	E_IO_STATE off_status;
};

static struct SWITCH_GPIO_S switch_gpio[SWITCH_MAX]={\
	{GPIOC, GPIO_Pin_11, GPIO_Mode_IPU, SWITCH_RESET},\
	{GPIOC, GPIO_Pin_12, GPIO_Mode_IPU, SWITCH_RESET},\
	{GPIOC, GPIO_Pin_8 , GPIO_Mode_Out_PP, SWITCH_RESET},\
	{GPIOD, GPIO_Pin_11, GPIO_Mode_Out_PP, SWITCH_RESET},\
	{GPIOD, GPIO_Pin_12, GPIO_Mode_Out_PP, SWITCH_RESET},\
};


void switch_driver_gpio_init(E_SWITCH index,E_IO_STATE off)
{
	if(index>=SWITCH_MAX)
		return;
	GPIO_InitTypeDef  GPIO_InitStructure = {0};

	switch_gpio[index].off_status = off;
	GPIO_InitStructure.GPIO_Pin = switch_gpio[index].GPIO_Pin;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_InitStructure.GPIO_Mode = switch_gpio[index].mode;
	GPIO_Init( switch_gpio[index].GPIOx, &GPIO_InitStructure );

	if((GPIO_Mode_Out_OD==switch_gpio[index].mode) || (GPIO_Mode_Out_PP==switch_gpio[index].mode))
		GPIO_WriteBit(switch_gpio[index].GPIOx, switch_gpio[index].GPIO_Pin, SWITCH_RESET==off ? Bit_RESET: Bit_SET);
}
void switch_driver_set_pin_on(E_SWITCH index)
{
	if(index>=SWITCH_MAX)
		return;
	if((GPIO_Mode_IPD==switch_gpio[index].mode) || (GPIO_Mode_IPU==switch_gpio[index].mode))
		return;
	BitAction pin_level = SWITCH_RESET==switch_gpio[index].off_status ? Bit_SET: Bit_RESET;
	GPIO_WriteBit(switch_gpio[index].GPIOx, switch_gpio[index].GPIO_Pin, pin_level);
}
void switch_driver_set_pin_off(E_SWITCH index)
{
	if(index>=SWITCH_MAX)
		return;
	if((GPIO_Mode_IPD==switch_gpio[index].mode) || (GPIO_Mode_IPU==switch_gpio[index].mode))
		return;
	BitAction pin_level = SWITCH_RESET==switch_gpio[index].off_status ? Bit_RESET: Bit_SET;
	GPIO_WriteBit(switch_gpio[index].GPIOx, switch_gpio[index].GPIO_Pin, pin_level);
}

u8 switch_driver_on_off(E_SWITCH index)
{
	BitAction pin_level;
	if(index>=SWITCH_MAX)
		return 0xff;
	if((GPIO_Mode_IPD==switch_gpio[index].mode) || (GPIO_Mode_IPU==switch_gpio[index].mode))
		pin_level = (BitAction)GPIO_ReadInputDataBit(switch_gpio[index].GPIOx, switch_gpio[index].GPIO_Pin);
	else
		pin_level = (BitAction)GPIO_ReadOutputDataBit(switch_gpio[index].GPIOx, switch_gpio[index].GPIO_Pin);
	BitAction off_level = SWITCH_RESET==switch_gpio[index].off_status ? Bit_RESET: Bit_SET;
	return !(pin_level==off_level);
}

void switch_driver_out_reversal_pin(E_SWITCH index)
{
	if(index>=SWITCH_MAX)
		return ;
	if((GPIO_Mode_IPD==switch_gpio[index].mode) || (GPIO_Mode_IPU==switch_gpio[index].mode))
		return ;
	BitAction pin_level = (BitAction)GPIO_ReadOutputDataBit(switch_gpio[index].GPIOx, switch_gpio[index].GPIO_Pin);
	GPIO_WriteBit(switch_gpio[index].GPIOx, switch_gpio[index].GPIO_Pin, Bit_SET == pin_level ? Bit_RESET: Bit_SET);
}

